package cn.tedu.ivoshw.user.service.impl;

import cn.tedu.ivoshw.base.exception.ServiceException;
import cn.tedu.ivoshw.base.response.StatusCode;
import cn.tedu.ivoshw.user.mapper.UserMapper;
import cn.tedu.ivoshw.user.pojo.dto.UserLoginParam;
import cn.tedu.ivoshw.user.pojo.dto.UserQuery;
import cn.tedu.ivoshw.user.pojo.dto.UserSaveParam;
import cn.tedu.ivoshw.user.pojo.entity.User;
import cn.tedu.ivoshw.user.pojo.vo.UserVO;
import cn.tedu.ivoshw.user.service.UserService;
import lombok.extern.slf4j.Slf4j;
import net.bytebuddy.implementation.bytecode.Throw;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Collections;
import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserMapper userMapper;


    @Override
    public List<UserVO> selectUser(UserQuery userQuery) {
        log.debug("查询业务，参数：{}", userQuery);
        List<UserVO> list=userMapper.selectUser(userQuery);
        log.debug(list.toString());
        return list;
    }

    @Override
    public UserVO login(UserLoginParam userLoginParam) {

        log.debug("登陆业务，参数：{}", userLoginParam);
        UserVO userVO=userMapper.selectByUserName(userLoginParam);
        if (userVO==null){
            throw new ServiceException(StatusCode.USERNAME_ERROR);
        }
        if(!userVO.getPassword().equals(userLoginParam.getPassword())){
            throw new ServiceException(StatusCode.PASSWORD_ERROR);
        }



        return userVO;
    }

    @Override
    public void saveUser(UserSaveParam userSaveParam) {
        log.debug("保存用户信息业务，参数{}", userSaveParam);
        User user=new User();
        BeanUtils.copyProperties(userSaveParam,user);
        if (user.getId()==null){//新增用户
            user.setPassword("123456");
            user.setCreateTime(new Date());
            userMapper.insertUser(user);

        }else {
            user.setUpdateTime(new Date());
            userMapper.update(user);
        }

    }

    @Override
    public void updateUserStatus(Long userId, String status) {
        log.debug("修改用户状态信息，参数{}，{}", userId, status);
        User user=new User();
        user.setId(userId);
        user.setStatus(status);
        user.setUpdateTime(new Date());
        userMapper.update(user);


    }

    @Override
    public void delete(Long id) {
        log.debug("删除用户，参数{}",id);
        userMapper.delete(id);
    }


}
